<?php
    include_once 'ns_include.php';
    include_once 'subapplication.class.php';

    class LoginHandler extends subapplication {
        private $cmd;
        public static $PROTOCOL = "Login";

        public static function AttemptLogin(command $cmd)
        {
            if (!self::IsThisClass($cmd)) return null;
            $N = new LoginHandler($cmd);
            return $N;
        }

        private $username = null;
        private $password = null;
        private $loginKey = null;

        public function __construct(command $cmd)
        {
            $this->cmd  = $cmd;
            $this->username = $cmd->GetData('USERNAME');
            $this->password = $cmd->GetData('PASSWORD');
        }

        public function execute()
        {
            $loginResult = ns_dao::authenticateUser($this->username, md5($this->password));
            switch ($loginResult){
                case (ns_dao::$AUTH_FAIL):
                    $this->UpdateResult("NS_PROTOCOL=AuthFailed");
                    break;

                case (ns_dao::$AUTH_OK):
                    $this->loginKey = getLoginKey();
                    //Update user table




                    
                    
                    $this->UpdateResult("NS_PROTOCOL=LoginSuccess&NS_LOGINKEY={$this->loginKey}");


                    break;
            }
        }

        private function getLoginKey(){
            //

            return "1564684468758";
        }


        public function result()
        {
            $this->cmd->GetResult();
        }

        public function UpdateResult($str)
        {
            $this->cmd->SetResult($str);
        }

        public static function IsThisClass(command $cmd)
        {
            if (!$cmd->HasDataKey('USERNAME')) return false;
            if (!$cmd->HasDataKey('PASSWORD')) return false;

            return true;
        }
    }
?>
